public class Solution02 {
	public String reverseStr(String s, int k) {
		char[] ch = s.toCharArray();
		// 每次走2k步，分2种情况进行反转
		for (int i = 0; i < ch.length; i += 2 * k) {
			// 剩余字符数大于或等于k，反转前k个
			if (i + k <= ch.length) {
				reverse(ch, i, i + k - 1);
				continue; // 没有走到数组结束，继续
			}
			// 剩余字符数小于k个，全部反转
			reverse(ch, i, ch.length - 1);
		}
		return new String(ch);
	}

	private void reverse(char[] ch, int start, int end) {
		while (start < end) {
			char tmp = ch[start];
			ch[start++] = ch[end];
			ch[end--] = tmp;
		}
	}
}
